<template>
	<view class="setting">
		<y-cell-item v-for='(item,index) in list' :key='index' :style="{'margin-bottom':index===0||index===2?'20rpx':'0'}"
		 :imgShow='false' :title='item.title' @tap='goTo(item)'>
		</y-cell-item>
		<view class="account" @tap='clearCache'>清理缓存</view>
		<view class="account" @tap='sheetShow=true'>退出</view>
		<u-action-sheet :list="sheetList" v-model="sheetShow" @click="handleExit"></u-action-sheet>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sheetShow: false,
				sheetList: [{
					text: '关闭应用'
				}, {
					text: '退出登录'
				}],
				list: [{
						title: '权限管理',
						url: '/pages/tabbar/my/setting/rootManage'
					}, {
						title: '修改头像',
						url: ''
					}, {
						title: '修改密码',
						url: '/pages/tabbar/my/setting/editPassword'
					}, {
						title: '关于',
						url: ''
					}, {
						title: 'UI库',
						url: '/pages/tabbar/my/setting/uiLab'
					}
				]
			};
		},
		created() {
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				uni.uploadFile({
					url: 'http://www.example.com/upload', //在这里输入修改头像的接口
					filePath: path,
					name: 'file',
					complete: (res) => {
						console.log(res);
					}
				});
			})
		},
		methods: {
			goTo(item) {
				if (item.title === '修改头像') {
					this.$u.route({
						url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
						params: {
							destWidth: 300,
							rectWidth: 200,
							fileType: 'jpg',
						}
					})
				} else {
					if (item.url) {
						uni.navigateTo({
							url: item.url
						})
					} else {
						uni.showToast({
							title: '暂未完成该功能...',
							icon: 'none'
						})
					}
				}
			},
			handleExit(index) {
				console.log(index)
				if (index === 0) {
					// #ifdef APP-PLUS  
					plus.runtime.quit(); // 执行关闭应用操作
					// #endif
				} else if (index === 1) {
					// #ifdef APP-PLUS
					this.fileWriter()
					//  #endif
					uni.reLaunch({
						url: '/pages/login/index'
					})
				}
			},
			fileWriter() {
				plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fobject) {
					fobject.root.getFile('token.txt', {
						create: true
					}, function(fileEntry) {
						fileEntry.file(function(file) {
							fileEntry.createWriter(function(writer) {
								writer.seek(0)
								writer.write('needLogin')
							}, function(e) {
								console.log(e)
							})
						})
					})
				})
			},
			clearCache() {
				//#ifdef APP-PLUS
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确定要清除缓存么？',
					success: function(res) {
						if (res.confirm) {
							let os = plus.os.name;
							if (os == 'Android') {
								let main = plus.android.runtimeMainActivity();
								let sdRoot = main.getCacheDir();
								let files = plus.android.invoke(sdRoot, "listFiles");
								let len = files.length;
								for (let i = 0; i < len; i++) {
									let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
									plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
										if (entry.isDirectory) {
											entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
												uni.showToast({
													title: '缓存清理完成',
													duration: 2000
												});
												that.formatSize(); // 重新计算缓存  
											}, function(e) {
												console.log(e.message)
											});
										} else {
											entry.remove();
										}
									}, function(e) {
										console.log('文件路径读取失败')
									});
								}
							} else { // ios  
								plus.cache.clear(function() {
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize();
								});
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				//#endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	.setting {
		height: 100vh;
		background-color: #ededed;

		.slot-value-img {
			height: 60rpx;
			width: 60rpx;
			border-radius: 100rpx;
		}

		::v-deep .u-drawer-content {
			border-radius: 20rpx 20rpx 0 0;
		}

		.account {
			margin-top: 20rpx;
			text-align: center;
			height: 80rpx;
			line-height: 80rpx;
			background-color: #fff;

			&:active {
				background-color: #ccc;
			}
		}
	}
</style>
